package com.banciyuan.bcywebview.biz.goods.shoppingcart;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.dialog.ConfirmDialog;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;


public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<DetailGoods> mCartGoodsList;
    private ImageLoader mImageLoader;

    public ShoppingCartAdapter(Context context, List<DetailGoods> cartGoodsList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mCartGoodsList = cartGoodsList;
        mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public ShoppingCartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ShoppingCartViewHolder(mLayoutInflater.inflate(R.layout.shoppingcart_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ShoppingCartViewHolder holder, int position) {
        DetailGoods cartGoods = mCartGoodsList.get(position);
        mImageLoader.displayImage(cartGoods.getCover(), holder.ivGoodsPic, ImageOptions.options);
        holder.tvGoodsPrice.setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(Float.valueOf(cartGoods.getBasic_price()) + Float.valueOf(cartGoods.getSelect_sku().getAdd_price()))));
        holder.tvGoodsStyle.setText(cartGoods.getSelect_sku().getDescription());
        holder.tvGoodsName.setText(cartGoods.getTitle());
        holder.tvGoodsNum.setText(String.valueOf(cartGoods.getCount()));
        holder.ivGoodsCheckStatus.setImageResource(cartGoods.is_select() ? R.drawable.ic_checkbox_pressed : R.drawable.ic_checkbox_normal);
        initAction(holder, cartGoods);
    }

    private void initAction(ShoppingCartViewHolder holder, DetailGoods cartGoods) {
        OnClick onClick = new OnClick(holder, cartGoods);
        holder.ivGoodsCheckStatus.setOnClickListener(onClick);
        holder.viewDeleteItem.setOnClickListener(onClick);
        holder.ivMinusNum.setOnClickListener(onClick);
        holder.ivPlusNum.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener {
        ShoppingCartViewHolder holder;
        DetailGoods cartGoods;

        public OnClick(ShoppingCartViewHolder holder, DetailGoods cartGoods) {
            this.holder = holder;
            this.cartGoods = cartGoods;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.goods_check_status:
                    Boolean tag = cartGoods.is_select();
                    holder.ivGoodsCheckStatus.setImageResource(tag ? R.drawable.ic_checkbox_normal : R.drawable.ic_checkbox_pressed);
                    cartGoods.setIs_select(!tag);
                    ((ShoppingCartActivity) mContext).setTotalCountAndPrice();
                    break;
                case R.id.iv_delete_item:
                    ConfirmDialog.Builder builder = new ConfirmDialog.Builder(mContext);
                    builder.setDescString(mContext.getString(R.string.confirm_delete_item_from_carts))
                            .setActionString(mContext.getString(R.string.mydialog_delete))
                            .setCancelString(mContext.getString(R.string.mydialog_cancel))
                            .setActionClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ShoppingCartHelper.deleteShoppingItem(mContext, cartGoods);
                                    mCartGoodsList.remove(cartGoods);
                                    notifyItemRemoved(holder.getLayoutPosition());
                                    ((ShoppingCartActivity) mContext).isShoppingCartEmpty();
                                    ((ShoppingCartActivity) mContext).setTotalCountAndPrice();
                                }
                            }).create().show();
                    break;
                case R.id.view_minus_goods:
                    if (cartGoods.getCount() > 1) {
                        cartGoods.setCount(cartGoods.getCount() - 1);
                        ShoppingCartHelper.deleteShoppingItemCount(mContext, cartGoods);
                        notifyItemChanged(holder.getLayoutPosition());
                        ((ShoppingCartActivity) mContext).setTotalCountAndPrice();
                    }
                    break;
                case R.id.view_plus_goods:
                    if (cartGoods.getCount() < 99) {
                        cartGoods.setCount(cartGoods.getCount() + 1);
                        ShoppingCartHelper.putShoppingItem(mContext, cartGoods);
                        notifyItemChanged(holder.getLayoutPosition());
                        ((ShoppingCartActivity) mContext).setTotalCountAndPrice();
                    }
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mCartGoodsList.size();
    }
}
